package com.example.javafx.bilibilidownload.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.TreeMap;

/**
 * @author zengbang
 * @create 2024-08-09 10:30
 **/
public class UrlUtil {

    private static final Logger log = LogManager.getLogger(UrlUtil.class);

    public static TreeMap<String, Object> getTreeMap(String url) {

        // 把url问号后面的参数解析成一个treeMap
        TreeMap<String, Object> parameterMap = new TreeMap<>();
        int questionMarkIndex = url.indexOf("?");
        if (questionMarkIndex != -1 && questionMarkIndex < url.length() - 1) {
            String query = url.substring(questionMarkIndex + 1);
            String[] parameters = query.split("&");
            for (String parameter : parameters) {
                String[] keyValue = parameter.split("=");
                if (keyValue.length == 2) {
                    String key = keyValue[0];
                    String value = keyValue[1];
                    parameterMap.put(key, value);
                }
            }
        }
        return parameterMap;
    }
}
